package com.loong.web.gp4j.view.compile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.loong.tool.lang.Strings;
import com.loong.web.gp4j.bean.param.code.CodeParam;
import com.loong.web.gp4j.bean.param.code.ExchangeParam;
import com.loong.web.gp4j.bean.schema.user.Auths;
import com.loong.web.gp4j.service.code.CodeService;
import com.loong.web.suppert.collection.vo.ResultVo;
import com.loong.web.suppert.collection.vo.ResultVoFactory;
import com.loong.web.suppert.intercept.auth.AuthType;

/**
 * 代码资源
 *
 * @author 张成轩
 */
@Controller
@RequestMapping("compile/codes")
public class CodesViewMapping {

	@Resource
	private CodeService codeService;

	/**
	 * 代码管理页面
	 * 
	 * @param request 请求
	 * @return 视图
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@AuthType(Auths.EDITOR)
	public ModelAndView codes(HttpServletRequest request) {

		ModelAndView model = new ModelAndView("gp4j/compile/codes");
		model.addObject("codes", codeService.getCodes());
		return model;
	}

	/**
	 * 获取代码
	 * 
	 * @param request 请求
	 * @param id id
	 * @return 返回对象
	 */
	@RequestMapping(value = "get", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.EDITOR)
	public ResultVo<?> get(HttpServletRequest request, Integer id) {

		if (id == null)
			ResultVoFactory.fail("id不能为空");
		return codeService.getCode(id);
	}

	/**
	 * 编辑代码
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param valid 校验器
	 * @return 返回对象
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.EDITOR)
	public ResultVo<?> edit(HttpServletRequest request, @Valid CodeParam param, BindingResult valid) {

		if (valid.hasErrors())
			return ResultVoFactory.fail(valid.getAllErrors().get(0).getDefaultMessage());
		if (param.getId() == null) {
			if (Strings.isNull(param.getType()))
				return ResultVoFactory.fail("代码类型不能为空");
			return codeService.add(param);
		} else {
			return codeService.edit(param);
		}
	}

	/**
	 * 移除代码
	 * 
	 * @param request 请求
	 * @param id id
	 * @return 返回对象
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.EDITOR)
	public ResultVo<?> remove(HttpServletRequest request, Integer id) {

		if (id == null)
			ResultVoFactory.fail("id不能为空");
		return codeService.remove(id);
	}

	/**
	 * 交换排序
	 * 
	 * @param request 请求
	 * @param param 参数
	 * @param valid 校验器
	 * @return 返回对象
	 */
	@RequestMapping(value = "exchange", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.EDITOR)
	public ResultVo<?> exchange(HttpServletRequest request, @Valid ExchangeParam param, BindingResult valid) {

		if (valid.hasErrors())
			return ResultVoFactory.fail(valid.getAllErrors().get(0).getDefaultMessage());
		return codeService.exchange(param);
	}
}
